package com.jkoss.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.jkoss.biz.IStusBiz;
import com.jkoss.bo.AjaxReturn;
import com.jkoss.pojo.kc.Kaoshi;
import com.jkoss.pojo.oa.Emps;
import com.jkoss.pojo.stu.Classes;
import com.jkoss.pojo.stu.Evaluate;
import com.jkoss.pojo.stu.Ossstu;
import com.jkoss.pojo.stu.Results;
import com.jkoss.pojo.zx.Consultation;
import com.jkoss.tool.DbCommonMethod;
import com.jkoss.tool.Page;

@Controller
@RequestMapping("/stu")
public class StudentsAction {
	
	@Autowired
	private IStusBiz stubiz;
	
	
	
	
	@RequestMapping("/liststu")
	public String listAllstu(HttpServletRequest req,Page<Ossstu> page,Integer clzid,Integer stustate,Integer pd){
		
		List<Ossstu> students=null;
		Map<String,Object> params=new HashMap<String,Object>();
		if(page==null){
			page=new Page<Ossstu>();
		}
		//检查application的班级
		if(req.getServletContext().getAttribute("classes")==null){
			List<Classes> classes= stubiz.listAllClass();
			req.getServletContext().setAttribute("classes", classes);
		}
		
		
		if(clzid!=null&&clzid>0){
			//根据班级查询
			req.getSession().removeAttribute("clzid");
			req.getSession().removeAttribute("stustate");
			params.put("clzid", clzid);
			page.setParams(params);
			students= stubiz.listAllStudents(page);
			req.getSession().setAttribute("clzid", clzid);
		}else if(stustate!=null){
			//查询状态
			req.getSession().removeAttribute("clzid");
			req.getSession().removeAttribute("stustate");
			params.put("stustate", stustate);
			page.setParams(params);
			students= stubiz.listAllStudents(page);
			req.getSession().setAttribute("stustate", stustate);
		}else if(pd!=null||clzid!=null&&clzid==0){
			
			//判断是不是按菜单的按钮进来查看学生的，是的话不用通过会话来判断
			req.getSession().removeAttribute("clzid");
			req.getSession().removeAttribute("stustate");
			students= stubiz.listAllStudents(page);	
		}else{
			
			//这段是点击分页，取会话里面保存的字段，确保是按什么id来分页，下面两个id总有一个是空的
			clzid= (Integer) req.getSession().getAttribute("clzid");
			params.put("clzid", clzid);
			stustate= (Integer) req.getSession().getAttribute("stustate");
			params.put("stustate", stustate);
			page.setParams(params);
			students= stubiz.listAllStudents(page);	
		}
		
		
		page.setResults(students);
		req.setAttribute("page", page);
		
		return "/stu/listStudents.jsp";
	}
	
	//分班，未分班的添加进班级
	@RequestMapping("/updateState")
	public String updateState(HttpServletRequest req,Page<Ossstu> page,String stuids,Integer clzid){
		String stuIds[]=null;
		
		if(page==null){
			page=new Page<Ossstu>();
		}
		if(stuids.length()>=1){
			stuids=stuids.substring(0, stuids.length()-1);
			stuIds=stuids.split("_");
		}
		for (String stuid : stuIds) {
			stubiz.updateByStuidClzid(stuid,clzid);
		}	
		return "/stu/listStudents.jsp";	
	}
	
	//页面登记学生的跳转
	@RequestMapping("/ToaddStu")
	public String ToaddStu(HttpServletRequest req,String stuname,String stuphone){

		Consultation con=null;
		Ossstu ossstu=new Ossstu();
		
		if(stuname!=null&&stuphone!=null){
			con=stubiz.selectByConsoluction(stuname,stuphone);
			if(con!=null){
				//如果咨询量里面有记录，就把该咨询量的信息传过去
				ossstu.setStuname(con.getStuname());
				ossstu.setStusex(con.getSsex());
				ossstu.setStucellphone(con.getStuphone());
				ossstu.setStustate(0);//登记的时候设计状态为未分班
				
				ossstu.setOssstuage(null);
				ossstu.setJoinDate(null);
				ossstu.setZstuid(null);//这里咨询表有的数据，加上咨询的关联，不用在前台显示
				
				req.getSession().setAttribute("zxstuid", con.getZstuid());//咨询的id放进会话，后面添加的时候加上该字段
				req.setAttribute("ossstu", ossstu);
			}else{
				//如果咨询量里面没有记录，就记为空
				ossstu.setStuname(null);
				ossstu.setStusex(null);
				ossstu.setStucellphone(null);
				ossstu.setStustate(0);//登记的时候设计状态为未分班
				
				ossstu.setOssstuage(null);
				ossstu.setJoinDate(null);
				
				req.getSession().setAttribute("zxstuid", 0);//咨询的id放进会话，后面添加的时候加上该字段
				req.setAttribute("ossstu", ossstu);

			}
		}

		return "/stu/listStudents.jsp";
	}
	
	//登记学生
	@RequestMapping("addstu")
	public String addstu(HttpServletRequest req,Ossstu ossstu,String sex){
		//如果咨询里面有信息，登记学员的信息里面也应该要有该约束
		int zxstuid=(int) req.getSession().getAttribute("zxstuid");
		if(zxstuid!=0){
			ossstu.setZstuid(zxstuid);
		}
		
		if("男".equals(sex)){
			ossstu.setStusex(1);
		}else if("女".equals(sex)){
			ossstu.setStusex(0);
		}
		ossstu.setStustate(0);//登记的都未分班状态
		
		//添加学员
		//根据姓名跟电话判断该学生有没有存在
		if(stubiz.selectStuByNamePhone(ossstu.getStuname(),ossstu.getStucellphone())==null){
			int tj=stubiz.insertOssstu(ossstu,zxstuid);
			if(tj>0){
				req.setAttribute("msg", "添加学员成功");
			}else{
				req.setAttribute("msg", "添加学员失败");
			}
		}else{
			req.setAttribute("msg", "该学员已经存在，请不要重复添加");
		}
		
		req.setAttribute("ossstu", null);;
	//	return  "redirect:/ liststu ";
		return listAllstu(req,null,null,null,1);
		
	}
	
	//班级管理
		@RequestMapping("/getAllClass")
		public String getAllClassa(HttpServletRequest req,Page<Classes> page,Integer eid){
			//用不上分页
			if(page==null){
				page=new Page<Classes>();
			}
			if(eid==null){
				Emps emp= (Emps) req.getSession().getAttribute("lgnUsr");
				eid=emp.getEid();
			}
			
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("eid", eid);
			page.setParams(params);
			
			page.setResults(stubiz.listAllClases(page));
			req.setAttribute("page", page);
			
			return "/stu/listClass.jsp";
			
		}
		

		////添加新班级
		@RequestMapping("/addCla")
		public String addClassAA(Classes cla,HttpServletRequest req){	
			//添加用户id
			Emps emp= (Emps) req.getSession().getAttribute("lgnUsr");
			cla.setEid(emp.getEid());

			req.setAttribute("msg", stubiz.addClass(cla));
			return getAllClassa(req,null,null);
		}
		
		
		
		//删除班级
		@RequestMapping("delCal")
		public String delClass(HttpServletRequest req,Integer clzid){
			if(stubiz.selectStuByclzid(clzid).size()==0){
				req.setAttribute("msg", stubiz.delCla(clzid));
			}else{
				req.setAttribute("msg", "该班级有学生，不能删除");
			}
			
			
			return getAllClassa(req,null,null);
			
		}
		//考勤管理
		@RequestMapping("shKqByeid")
		public String shKqByeid(HttpServletRequest req,Integer eid){
		//	if(eid)
			return null;
		}
		//添加学生评价的时候，先查询该学生的信息
		@RequestMapping("shPjstu")
		@ResponseBody
		public AjaxReturn shpjstu(HttpServletRequest req,int ossstuid){
			AjaxReturn ajaxReturn=new AjaxReturn();
			
			
			Ossstu ossstu= stubiz.seletStuByOsid(ossstuid);
	//		System.out.println(ossstu.getEname()+ossstu.getStuname());
			if(ossstu!=null){
				ajaxReturn.setHead("ok");
				ajaxReturn.setBody(ossstu);
			}else{
				ajaxReturn.setHead("err");
			}
			return ajaxReturn;	
		}
		
		//添加学员评价
		@RequestMapping("addEvaluete")
		public String addEvaluete(HttpServletRequest req,Evaluate eva){
			
			req.setAttribute("msg", stubiz.addEva(eva));
			return listAllstu(req,null,null,null,1);
		}
		
		//查看该生所有评价
		@RequestMapping("shAllEval")
		@ResponseBody
		public String shAllEval(HttpServletRequest req,int ossstuid,Page<Evaluate> page){
			req.getSession().removeAttribute("EvaList");
			if(page==null){
				page=new Page<Evaluate>();
			}
			
			List<Evaluate> list= stubiz.selectEvalByOssstuId(ossstuid);
			System.out.println(list.size());
			
			//这里要改，不要存到会话
			//通过ajax将查询到的返回到会话
			
			req.getSession().setAttribute("EvaList", list);
			
			return JSONObject.toJSONString(list).toString();
			
		}
		
		//查看该生所有评价2
				@RequestMapping("shAllEval2")
				@ResponseBody
				public AjaxReturn shAllEval2(HttpServletRequest req,int ossstuid,Page<Evaluate> page){
			
					AjaxReturn ajaxReturn=new AjaxReturn();
					if(page==null){
						page=new Page<Evaluate>();
					}
					List<Evaluate> list= stubiz.selectEvalByOssstuId(ossstuid);
					System.out.println(list.size());
					if (list.size()>0&&list!=null) {
						ajaxReturn.setHead("ok");
						ajaxReturn.setBody(list);
					}
					
					/*req.getSession().removeAttribute("EvaList");
					if(page==null){
						page=new Page<Evaluate>();
					}
					
					List<Evaluate> list= stubiz.selectEvalByOssstuId(ossstuid);
					System.out.println(list.size());
					
					//这里要改，不要存到会话
					//通过ajax将查询到的返回到会话
					
					req.getSession().setAttribute("EvaList", list);
					
					return JSONObject.toJSONString(list).toString();*/
					return ajaxReturn;
					
					
				}
		
				
		//考核主题
		@RequestMapping("toaddKaohe")
		public String toaddKaohe(HttpServletRequest req,int clzid){
			req.setAttribute("clzid", clzid);
			return "/stu/addClassKaohe.jsp";
		}
		//添加考核主题到班级
		@RequestMapping("upkaohe")
		public String upkaohe(MultipartFile upkszt,HttpServletRequest req,int clzid,Integer empid){
			
			if(empid==null){
				Emps emp=(Emps) req.getSession().getAttribute("lgnUsr");
				empid=emp.getEid();
			}
			
			if(upkszt!=null){
				String kaohename=upkszt.getOriginalFilename();//得到文件名
				
				
				String basepath="E:/oss附件/考试数据";//存到这个地址
				
				//物理保存
				try {
					File file=new File(basepath, kaohename);
					upkszt.transferTo(file);
					
					//解析入库，添加到数据库
					System.out.println(kaohename+"添加成功");
					
					req.setAttribute("msg", stubiz.addkaoshi(file.getAbsolutePath(),clzid,empid));

				} catch (IllegalStateException | IOException e ) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
			
			return "/stu/addClassKaohe.jsp";
		}
		
		//班级考试评价
		@RequestMapping("ClassKspj")
		@ResponseBody
		public AjaxReturn ClassKspj(HttpServletRequest req,int clzid){	
			AjaxReturn ajaxReturn=new AjaxReturn();
			//查询该班级的所有考核主题
			List<Kaoshi> kslist= stubiz.selectKaoshiByClzid(clzid);
			if(kslist!=null&&kslist.size()>0){
				ajaxReturn.setHead("ok");
				ajaxReturn.setBody(kslist);
			}else{
				ajaxReturn.setHead("err");
				//查找班级名称返回去
				Classes c= stubiz.selectClznameByClzid(clzid);
				ajaxReturn.setBody(c.getClzname());
			}
		//	System.out.println(ajaxReturn);
		//	req.setAttribute("kslist", kslist);
			return ajaxReturn;
		}
		
		//添加班级考核评价和备注
		@RequestMapping("addKhpjbz")
		public String addKhpjbz(HttpServletRequest req,Kaoshi ks){

			req.setAttribute("msg", stubiz.upadateKspjAndBz(ks));
			return getAllClassa(req,null,null);	
		}
		
		//返回需要的考核主题跟学生名字到添加成绩的页面
	
		@RequestMapping(value = "/selectKhStuname",produces={"application/json;","text/html;charset=UTF-8;"})  
	//	@RequestMapping("/selectKhStuname")
		@ResponseBody
		public AjaxReturn selectKhStuname(int ossstuid){
			AjaxReturn ajaxReturn=new AjaxReturn();
			
			List<Kaoshi> kslist= stubiz.selectKhStuname(ossstuid);
			
			if(kslist.size()>0&&kslist!=null){
				ajaxReturn.setHead("ok");
				ajaxReturn.setBody(kslist);
			}else{
				ajaxReturn.setHead("err");
				ajaxReturn.setBody("没有考核");
			}

			return ajaxReturn;
		}
		
		//添加学生考核成绩评价
		@RequestMapping("addStuChengji")
		public String addStuChengji(HttpServletRequest req,Results rs){
			//System.out.println(rs.getOssstuid()+" "+rs.getScore()+"  "+rs.getThemeid()+"  "+rs.getScoredescb());
			req.setAttribute("msg", stubiz.addStuCj(rs)); 
			return listAllstu(req,null,null,null,1);			
		}
		
		//去修改学生信息页面
		@RequestMapping("toupdatastu")
		public String updatastu(HttpServletRequest req,int stuid){
					
			if(stuid>0){
				req.setAttribute("ossstu",  stubiz.seletStuByOsid(stuid));
			}
					
			return "upossstu.jsp";
					
		}
		//修改学生信息
		@RequestMapping("uptOssstu")
		public String uptOssstu(HttpServletRequest req,Ossstu stu,int ossstuid){
		//	System.out.println(ossstuid);
			stu.setOssstuid(ossstuid);
		//	System.out.println(stu.getStuname()+" "+stu.getOssstuid()+" "+stu.getJoinDate());
			req.setAttribute("msg", stubiz.updateByOssstu(stu));
					
			return listAllstu(req,null,null,null,null);		
		}
}
